<?php

use yii\helpers\Url;
use common\models\Article;
$this->addCssFile([
    "plugins/webuploader/webuploader.css"
]);

$this->addJsFile([
//    "plugins/ueditor/ueditor.config.js",
//    "plugins/ueditor/ueditor.all.min.js",
    "plugins/webuploader/webuploader.min.js",
    "js/webupload.js"
]);

$this->inline("title","添加文章");
$request = Yii::$app->request->post();
?>
<div class="panel">
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box">
                    <h4 class="m-t-0 m-b-30 header-title"><b>添加文章</b></h4>
                    <p class="text-muted font-13 m-b-30">
                        爱生活、爱分享、爱编程！
                    </p>
                    <form id="article" onclick="return false" enctype="multipart/form-data">
                        <input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->csrfToken?>">
                        <div class="form-group">
                            <label>文章标题</label>
                            <input type="text" name="title" placeholder="请输入文章标题" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>所属分类</label>
                            <select class="form-control" name="category_id">
                                <?php foreach ($category as $cate):?>
                                <option <?php echo isset($request['category_id']) && $request['category_id'] == $cate['id']?"selected":"";?> value="<?php echo $cate['id']?>"><?php echo "├─".$cate['html'].$cate['cname']?></option>
                                <?php endforeach;?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>关键字</label>
                            <input type="text" name="keywords" value="<?php echo isset($request['keywords'])?$request['keywords']:"";?>" placeholder="关键字：爱生活、爱分享" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>描 述</label>
                            <textarea class="form-control" name="desc" placeholder="描述：这是一个神奇的网站"><?php echo isset($request['desc'])?$request['desc']:"";?></textarea>
                        </div>
                        <div class="form-group">
                            <label>上传缩略图：</label>
                            <div id="fileSelect" class="waves-effect waves-light">选择图片</div>
                            <input type="hidden" name="thumb" value="">
                        </div>
                        <div class="form-group">
                            <img style="width: 300px;height: 150px;border: 1px dashed rgba(0, 0, 0, 0.1);padding: 5px;" src="<?php echo isset($request['thumb']) ? $request['thumb']:''?>" class="img-responsive uploadImg">
                        </div>
                        <div class="form-group">
                            <label>标签<span style="font-weight: 100;color: red"> *用逗号(,)隔开</span></label>
                            <input type="text" name="tags" value="<?php echo isset($request['tags'])?$request['tags']:"";?>" placeholder="标签：爱生活,爱分享" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>内容</label>
                            <textarea id="editor" name="content"><?php echo isset($request['content'])?$request['content']:"";?></textarea>
                        </div>
                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($request['status']) && $request['status'] == 0?"checked":"";?> type="checkbox" value="0" name="status"><label>提交审核</label>
                        </div>
                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($request['status']) && $request['status'] == 1?"checked":"";?> type="checkbox" value="1" name="status"><label>申请置顶</label>
                        </div>
                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($request['status']) && $request['status'] == 2?"checked":"";?> type="checkbox" value="2" name="status"><label>立即发布</label>
                        </div>
                        <div class="form-group text-right m-b-0">
                            <button onclick="saveObj.save()" class="btn btn-primary waves-effect waves-light" type="button">
                                提 交
                            </button>
                            <button type="reset" class="btn btn-default waves-effect waves-light m-l-5">
                                重 置
                            </button>
                            <button onclick="location.href='<?=Url::toRoute('/article')?>'" type="button" class="btn btn-default waves-effect waves-light m-l-5">
                                返 回
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock("jsText","append");?>
<script type="text/javascript">
    var formData = {};
    var csrfToken = $('meta[name="csrf-token"]').attr("content");
    var csrfParam = $('meta[name="csrf-param"]').attr("content");

    // 请求数据
    formData[csrfParam] = csrfToken;
    formData['category'] = 'image';
    formData['action'] = 'article';

    // 上传插件初始化
    var option = {
        pick: {id: '#fileSelect', multiple: false},
        formData: formData, //category:image 图片 document 文档  media 媒体
        server:'<?php echo Yii::$app->urlManager->createUrl(['api/upload/index'])?>',
        fileSingleSizeLimit: 10 * 1024 * 1024,
        accept: {
            title: 'Image',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'Image/gif,Image/jpeg,Image/bmp,Image/png'
        },
        func: {
            uploadSuccess: function (file, args)
            {
                var url = '<?php echo $this->imghost?>'+args.path;
                if (args.state === 'SUCCESS') {
                    $(".uploadImg").attr('src',url);
                    $("input[name=thumb]").val(args.path);
                } else {
                    console.log(file);
                }
            }
        }
    };
    uploadObject.init(option);

    //编辑器初始化
    var ue = UE.getEditor('editor',{
        initialFrameHeight:400,
        autoHeightEnabled: true,
        autoFloatEnabled: true
    });


    var saveObj = {
        ajaxLock:false,
        save:function () {
            that = this;
            var data = $("#article").serialize();
            var submitUrl = "<?php echo Yii::$app->urlManager->createUrl(['article/save'])?>";

            if (that.ajaxLock)return false;
            that.ajaxLock = true;

            $.ajax({
                data:data,
                url:submitUrl,
                type:'post',
                dataType:'json',
                success:function (resp) {
                    $.Notification.autoHideNotify(resp.state, 'bottom right', '提示信息',resp.message);
                    that.ajaxLock = false;

    },
    error:function () {
    that.ajaxLock = false;
                }
            });
        }
    };
    </script>
<?php $this->endBlock();?>